package net.wanglu.www.zzz.main;

import java.util.HashMap;

import net.wanglu.www.zzz.utils.LoggerFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public abstract class WangluMain {

    protected DataBufferPoolManager dpm;
    protected ErrorHandler errorHandler;
    protected HashMap<String, Runnable> tasks;
    
    public HashMap<String, Runnable> getTasks() {
		return tasks;
	}
	public void setTasks(HashMap<String, Runnable> tasks) {
		this.tasks = tasks;
	}
	
	public ErrorHandler getErrorHandler() {
    	return this.errorHandler;
    }
	public void setErrorHandler(ErrorHandler errorHandler) {
		this.errorHandler = errorHandler;
	}
	
	public DataBufferPoolManager getDpm() {
		return this.dpm;
	}
	public void setDpm(DataBufferPoolManager dpm) {
		this.dpm = dpm;
	}
	
	public static void initSys(String configPath) throws Exception {
		Configer.loadProperties(configPath);
	}
	
	public static WangluMain getMain(String concreteMain) throws Exception {
        String appXmls[] = Configer.getConfiger().getSpring();
        ApplicationContext ctx = new FileSystemXmlApplicationContext(appXmls);
        return (WangluMain) ctx.getBean(concreteMain);
	}
	
	public static boolean startUp(String[] args) throws Exception {
        try
        {
            if(args.length < 1)
                return false;
        }
        catch(Exception e)
        {
        	LoggerFactory.SelfLogger.error("",e);
            return false;
        }

        try {
			initSys(args[0]);
			LoggerFactory.load(Configer.getConfiger().getLog4j());
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public abstract void warmUp() throws Exception;
	
	public abstract void allocatePool() throws Exception;
	
	public abstract void allocateTask() throws Exception;
}
